﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;
using System.Data.Entity.Validation;

namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageCaseLibController : Controller
    {
        private MyPSEntities db = new MyPSEntities();

        //
        // GET: /Manage/ManageCaseLib/

        public ActionResult Index()
        {
            return View(db.CaseBases.OrderByDescending(o=>o.CreateDate).ToList());
        }

       
      

        //
        // GET: /Manage/ManageCaseLib/Create

        public ActionResult Create()
        {
            ViewBag.TypeList = new SelectList(db.CaseTypes.ToList(), "CaseTypeName", "CaseTypeName");
            return View();
        }

        //
        // POST: /Manage/ManageCaseLib/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(CaseBas casebas)
        {
            if (ModelState.IsValid)
            {
                var file = Request.Files["fileImgUrl"];
                if (file != null)
                {
                    try
                    {
                        if (file.ContentLength > 204800)
                        {
                            throw new Exception("封面图片超过200kb");
                        }
                        string ex = System.IO.Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid().ToString() + ex;
                        string path = Server.MapPath("~/ImgCase/");
                        casebas.CoverUrl = string.Format("ImgCase/{0}", fileName);
                        file.SaveAs(path + fileName);
                        casebas.CreateBy = HttpContext.User.Identity.Name;
                        casebas.CreateDate = DateTime.Now;
                        casebas.ModifyBy = HttpContext.User.Identity.Name;
                        casebas.ModifyDate = DateTime.Now;
                        casebas.TypeID = 0;
                        var obj = db.CaseBases.Add(casebas);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    catch (DbEntityValidationException ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e.Message);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "未找到封面图片");
                }
            }
            ViewBag.TypeList = new SelectList(db.CaseTypes.ToList(), "CaseTypeName", "CaseTypeName");
            return View(casebas);
        }

        //
        // GET: /Manage/ManageCaseLib/Edit/5

        public ActionResult Edit(int id)
        {
            CaseBas casebas = db.CaseBases.Find(id);
            ViewBag.TypeList = new SelectList(db.CaseTypes.ToList(), "CaseTypeName", "CaseTypeName", casebas.TypeName);
            if (casebas == null)
            {
                return HttpNotFound();
            }
            return View(casebas);
        }

        //
        // POST: /Manage/ManageCaseLib/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(CaseBas casebas)
        {
            if (ModelState.IsValid)
            {
                casebas.ModifyBy = HttpContext.User.Identity.Name;
                casebas.ModifyDate = DateTime.Now;
                db.Entry(casebas).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeList = new SelectList(db.CaseTypes.ToList(), "CaseTypeName", "CaseTypeName", casebas.TypeName);
            return View(casebas);
        }

        //
        // GET: /Manage/ManageCaseLib/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CaseBas casebas = db.CaseBases.Find(id);
            if (casebas == null)
            {
                return HttpNotFound();
            }
            return View(casebas);
        }

        //
        // POST: /Manage/ManageCaseLib/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CaseBas casebas = db.CaseBases.Find(id);
            db.CaseBases.Remove(casebas);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}